package logic

import (
	"context"
	"fmt"
	"strconv"
	"strings"

	"mall/service/recommended/api/internal/svc"
	"mall/service/recommended/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetItemPopularLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetItemPopularLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetItemPopularLogic {
	return &GetItemPopularLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetItemPopularLogic) GetItemPopular(req *types.ItemSearchReq) (
	resp *types.NeighborsResp, err error,
) {
	resp = &types.NeighborsResp{}
	if len(strings.TrimSpace(req.UserId)) == 0 {
		return resp, nil
	}

	pageSize, err := strconv.ParseInt(req.PageSize, 10, 64)
	if err != nil {
		logx.Errorf("GetItemPopular pageSize ParseInt Error: %+v", err)
		return nil, err
	}
	offset, err := strconv.ParseInt(req.Offset, 10, 64)
	if err != nil {
		logx.Errorf("GetItemPopular offset ParseInt Error: %+v", err)
		return nil, err
	}
	res, err := l.svcCtx.GorseClient.GetItemPopular(l.ctx, req.UserId, int(pageSize), int(offset))
	if err != nil {
		fmt.Println("GetItemPopular Error: ", err)
		return resp, err
	}
	for _, v := range res {
		resp.List = append(resp.List, map[string]any{
			"id":    v.Id,
			"score": v.Score,
		})
	}
	fmt.Println(resp.List, "list")
	return resp, nil
}
